﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace CombatLogSplitter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           
                OpenFileDialog input = new OpenFileDialog();
                input.ShowDialog();
                int dayCounter = 0;
                
                if (input.FileName != null)
                {
                    StreamReader file = new StreamReader(input.FileName);
                    try
                    {
                        

                        string line = String.Empty;
                        string lastDate = " ";

                        while ((line = file.ReadLine()) != null)
                        {
                            if (!line.StartsWith(lastDate))
                            {
                                string[] temp = line.Split(' ');
                                lastDate = temp[0];

                                //new date means new file                            
                               

                                StreamWriter newFile = new StreamWriter(System.IO.Path.GetDirectoryName(input.FileName) + "\\Day" + dayCounter + ".txt");
                                dayCounter++;
                                newFile.WriteLine(line);

                                while ((line = file.ReadLine()).StartsWith(lastDate))
                                {
                                    newFile.WriteLine(line);
                                }

                                newFile.Close();
                            }
                        }
                    }
                    finally
                    {
                        file.Close();
                    }
                }
            

        }
    }
}
